package com.example.mychat.Controller;

import com.example.mychat.entity.Authorities;
import com.example.mychat.entity.Users;
import com.example.mychat.mapper.AuthoritiesMapper;
import com.example.mychat.mapper.UsersMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.security.Principal;

@Tag(name = "用户管理")
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UsersMapper usersMapper;
    @Resource
    AuthoritiesMapper authoritiesMapper;

    @Operation(description = "查看用户Id")
    @GetMapping("/getUserId")
    @ResponseBody
    String getUserId(HttpSession session) {
        String username = String.valueOf(session.getAttribute("username"));
        if (username.equals("null")) {
            return "用户未登录";
        } else {
            return username;
        }
    }

    @Operation(description = "用户注册")
    @PostMapping("/register")
    @ResponseBody
    String register(@RequestParam String username, @RequestParam String password) {
        Users users = usersMapper.selectById(username);
        if (users != null) {
            return "用户名已存在";
        } else {
//            PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            users = new Users(username, password);
            Authorities authorities = new Authorities(username, "USER");
            authoritiesMapper.insert(authorities);
            usersMapper.insert(users);
            return "注册成功";
        }
    }

    @Operation(description = "用户登录")
    @PostMapping("/login")
    @ResponseBody
    String logIn(@RequestParam String username, @RequestParam String password, HttpSession session) {
        Users users = usersMapper.selectById(username);
        if (users == null) {
            return "用户未注册";
        } else if (users.getUsername().equals(username) && users.getPassword().equals(password)) {
            String Flag = String.valueOf(session.getAttribute("username"));
            if (!Flag.equals("null")) {
                session.removeAttribute("username");
            }
            session.setAttribute("username", username);
            return "登录成功";
        } else {
            return "用户名或密码错误";
        }
    }

    @Operation(description = "退出登录")
    @PostMapping("/logout")
    @ResponseBody
    String logOut(HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username == null) {
            return "用户未登录";
        } else {
            session.removeAttribute("username");
            return "退出成功";
        }
    }

}
